/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.Date;
import model.Thanhtoan;
import model.Thanhvien;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author HoangLiem
 */
public class ThanhToanDAO {

    public static ArrayList<Thanhtoan> getAllPayments() {
        ArrayList<Thanhtoan> list = new ArrayList<Thanhtoan>();
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Thanhtoan";
            Query query = session.createQuery(hql);
            list = (ArrayList<Thanhtoan>) query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static ArrayList<Thanhtoan> getAllPaymentsByUser(Thanhvien thanhvien) {
        ArrayList<Thanhtoan> list = new ArrayList<Thanhtoan>();
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Thanhtoan tt where tt.thanhvien =:thanhvien";
            Query query = session.createQuery(hql);
            query.setEntity("thanhvien", thanhvien);
            list = (ArrayList<Thanhtoan>) query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static Boolean insertThanhToan(String username, String masp) {
        Boolean kq = true;
        Thanhtoan tt = new Thanhtoan();
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        tt.setThanhvien(ThanhVienDAO.getUserById(username));
        tt.setSanpham(SanphamDAO.getProduct(masp));
        tt.setNgayThanhToan(new java.util.Date());
        try {
            if (getThanhToan(username, masp) == null) {
                trans.begin();
                ss.merge(tt);
                ss.flush();
                trans.commit();
            }
        } catch (HibernateException ex) {
            kq = false;
            System.out.println(ex.getMessage());
        } finally {
            ssFac.close();
            return kq;
        }
    }

    public static Boolean capNhatTinhTrang(int tinhtrang, String username, String masp) {
        Boolean kq = true;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        try {
            Thanhtoan tt = getThanhToan(username, masp);
            tt.setTinhTrang(tinhtrang);
            trans.begin();
            ss.merge(tt);
            ss.flush();
            trans.commit();
        } catch (HibernateException ex) {
            kq = false;
            System.out.println(ex.getMessage());
        } finally {
            ssFac.close();
            return kq;
        }
    }

    public static Thanhtoan getThanhToan(String username, String masp) {
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        Thanhtoan tt = new Thanhtoan();
        try {
            trans.begin();
            String hql = "from Thanhtoan tt where tt.thanhvien.tenDangNhap =:thanhvien and tt.sanpham.maSanPham = :masp";
            Query query = ss.createQuery(hql);
            query.setString("thanhvien", username);
            query.setString("masp", masp);
            tt = (Thanhtoan) query.uniqueResult();
            ss.flush();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            ssFac.close();
            return tt;
        }
    }
}
